package extractor;

import java.util.ArrayList;
import java.util.HashMap;

public class Analyzer {
	String convertTimeToString(String created_at){
		String result = "";
		HashMap months = new HashMap();
		months.put("Jan", "01");
		months.put("Feb", "02");
		months.put("Mar", "03");
		months.put("Apr", "04");
		months.put("May", "05");
		months.put("Jun", "06");
		months.put("Jul", "07");
		months.put("Aug", "08");
		months.put("Sep", "09");
		months.put("Oct", "10");
		months.put("Nov", "11");
		months.put("Dec", "12");
		String time = "";
		String month = "";
		String date = "";
		
		if (created_at.indexOf(":") > 0){
			time = created_at.substring(created_at.indexOf(":")-2,25) ;
			time = time.replaceAll(":","");
		} else {
			System.err.print("there is no time!");
			new Exception("no time!");
		}
		result = created_at.substring(12,16);
		date = created_at.substring(5,7);
		month = (String) months.get(created_at.substring(8,11));
		result = result + month + date + time;
		return result;
	}

	public ArrayList fromJSONObjectToString(String temp) {
		ArrayList result = null;
		if (temp.indexOf('[') > 0 ) { 
			temp = temp.substring(temp.indexOf('[') + 1 , temp.indexOf("],\"results_per_page\""));
			String twitter = temp;
			String time = "";
			String message = temp;
			int endIndex = 0;
			result = new ArrayList();
			boolean hasMessage = true;
			for (int i = 0; i < 15; i++) {
				hasMessage = true;
				if (temp.indexOf(",\"to_user_id\":")== -1 && temp.indexOf(",\"to_user\":")== -1) hasMessage = false;
				
				if ( temp.indexOf(",\"to_user_id\":") != -1 && (temp.indexOf(",\"to_user\":") == -1 || temp.indexOf(",\"to_user_id\":") < temp.indexOf(",\"to_user\":"))) {
					endIndex = temp.indexOf(",\"to_user_id\":");
				} else { 
					endIndex = temp.indexOf(",\"to_user\":");
				}
				if (hasMessage) {
					time = temp.substring(temp.indexOf("\"created_at\":")+14 , temp.indexOf("\"from_user\":")-2);
					time = convertTimeToString(time);
					message = temp.substring(Integer.valueOf(temp.indexOf("\"text\":")) + 8 , endIndex - 1);
					result.add(time + "$$$" + message);
					temp = temp.substring(endIndex + 30, temp.length());
				}
			}
		}
		return result;
	}
}
